/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.mallow.impl.model.vot;

import java.io.InputStream;
import java.io.IOException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.starlink.votable.VOElement;
import uk.ac.starlink.votable.VOElementFactory;
import uk.ac.starlink.votable.VOStarTable;
import uk.ac.starlink.votable.TableElement;

/**
 * Base class for a VOTable JoinTagParser.
 *
 */
public abstract class VOTableJoinTagParser
extends StarTableJoinTagParser
    {
    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(VOTableJoinTagParser.class);

    /**
     * Public constructor.
     *
     */
    public VOTableJoinTagParser(InputStream src)
        {
        super();
        this.init(src);
        }

    /**
     * Initialise our VOTable parser from an InputStream.
     * This can be overriden or added to by sub-classes.
     *
     */
    protected void init(InputStream src)
        {
        try {
            this.init(
                new VOElementFactory().makeVOElement(
                    src,
                    null
                    )
                );
            }
        catch (SAXException ouch)
            {
            log.error("SAXException while processing VOTable [" + ouch.getMessage() + "]");
            }
        catch (IOException ouch)
            {
            log.error("IOException while processing VOTable [" + ouch.getMessage() + "]");
            }
        }    

    /**
     * Process the top level VOTable element.
     *
     */
    protected void init(VOElement top)
    throws IOException
        {
        log.debug("VOTableJoinTagParser.init(VOElement)");
        //
        // Find the RESOURCE elements using standard DOM method.
        NodeList resources = top.getElementsByTagName(
            "RESOURCE"
            );
        //
        // Process the first resource in the tree.
        if (resources.getLength() > 0)
            {
            // 
            // Get the TABLE elements using VOElement method.
            VOElement resource = (VOElement) resources.item(0);
            VOElement[] tables = resource.getChildrenByName("TABLE");
            //
            // Process the first table element as a StarTable.
            if (tables.length > 0)
                {
                this.init(
                    new VOStarTable(
                        (TableElement) tables[0]
                        )
                    );
                }
            }
        }
    }

